package com.kawa.kemanage.controller;

import com.kawa.kemanage.service.UserReportService;
import com.kawa.kemanage.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping({"/api/report", "/app/report"})
public class UserReportController {
    @Autowired
    private UserReportService userReportService;

    /**
     * 修改举报处理状态
     *
     * @param params
     * @return
     */
    @RequestMapping("/handleReport")
    public Result handleReport(@RequestBody Map<String, Object> params) {
        Integer reportId = (Integer) params.get("reportId");
        Integer userId=(Integer) params.get("userId");
        String status = (String) params.get("status");
        String resolution = (String) params.get("resolution");

        if (userReportService.handleReport(reportId, status, resolution,userId)) {
            return Result.success();
        }
        return Result.error("处理失败");
    }

    /**
     * 撤销举报
     *
     * @param params
     * @return
     */
    @RequestMapping("/cancelReport")
    public Result cancelReport(@RequestBody Map<String, Object> params) {
        Integer reportId = (Integer) params.get("reportId");
        Integer userId=(Integer) params.get("userId");
        if (userReportService.cancelReports(reportId,userId)) {
            return Result.success();
        }
        return Result.error("撤销失败");
    }



    /**
     * 批量删除举报信息（支持单个删除）
     * @param params
     * @return
     */
    @RequestMapping("/batchDeleteReport")
    public Result batchDeleteReport(@RequestBody Map<String, Object> params) {
        List<Integer> reportIds = (List<Integer>) params.get("reportIds");
        if (userReportService.batchDeleteReport(reportIds)) {
            return Result.success();
        }
        return Result.error("批量删除失败");
    }

    /**
     * 根据条件查询
     * @param pageNo 页码
     * @param pageSize 每页条数
     * @param reason 举报理由
     * @param status 处理状态
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return
     */
    @RequestMapping("/userReport")
    @Cacheable(value = "report", key = "'reason-' + #reason + '-' + #status + '-' + #pageNo + '-' + #pageSize", unless = "#result == null")
    public Result queryUserReport(
            @RequestParam(required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String reason,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate
    ) {
        return Result.success(userReportService.queryReport(pageNo, pageSize, reason, status, startDate, endDate));
    }


}
